import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_xyf/pages/component/app_bar/index.dart';

import 'scroll_list.dart';

class ScrollDemo extends StatefulWidget {
  ScrollDemo({Key? key}) : super(key: key);

  @override
  _ScrollDemoState createState() => _ScrollDemoState();
}

class _ScrollDemoState extends State<ScrollDemo> {
  Future<List<String>> fetchData() async {
    await Future.delayed(Duration(seconds: 2));
    return List.generate(20, (index) => (index * 2).toString());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        title: '列表',
        actionName: '刷新',
        onPressed: () {
          scrollListKey.currentState!.handleRefresh();
        },
      ),
      body: ScrollList(
        key: scrollListKey,
        req: fetchData,
        childFun: (index, data) {
          return ListItem(data: data);
        },
      ),
    );
  }
}

class ListItem extends StatefulWidget {
  final data;
  ListItem({Key? key, this.data}) : super(key: key);

  @override
  _ListItemState createState() => _ListItemState();
}

class _ListItemState extends State<ListItem>
//  with WidgetsBindingObserver
{
  @override
  void initState() {
    // TODO: implement initState
    // super.initState();
    // WidgetsBinding.instance
    //     ?.addPostFrameCallback((_) => {print(context.size?.height)});
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {},
      child: Container(
        padding: EdgeInsets.all(10),
        color: Colors.white,
        child: Column(
          children: [
            Text(widget.data.toString()),
            Text(widget.data.toString()),
          ],
        ),
      ),
    );
  }
}
